package com.jinzhi.jzweb.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.ExaminationAllocationDO;
import com.jinzhi.jzweb.domain.ExaminationBatchDO;
import com.jinzhi.jzweb.domain.ExaminationRoomDO;
import com.jinzhi.jzweb.domain.OrganizationNewsDO;
import com.jinzhi.jzweb.service.ExaminationAllocationService;
import com.jinzhi.jzweb.service.ExaminationBatchService;
import com.jinzhi.jzweb.service.ExaminationRoomService;
import com.jinzhi.jzweb.service.OrganizationNewsService;
import com.jinzhi.sys.domain.UserDO;
import com.jinzhi.sys.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 
 * <pre>
 * 考场分配
 * </pre>
 * <small> 2020-02-24 21:00:12 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/examinationAllocation")
@Api(value = "教师组竞赛平台", tags = {"考场分配"})
public class ExaminationAllocationController extends BaseController {
	@Autowired
	private ExaminationAllocationService examinationAllocationService;
	@Autowired
	private UserService userService;
	@Autowired
	private ExaminationRoomService examinationRoomService;
	@Autowired
	private ExaminationBatchService examinationBatchService;
	@Autowired
	private OrganizationNewsService organizationNewsService;
	
	@GetMapping()
	@RequiresPermissions("jzweb:examinationAllocation:examinationAllocation")
	String ExaminationAllocation(){
	    return "jzweb/examinationAllocation/examinationAllocation";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:examinationAllocation:examinationAllocation")
	public Result<Page<ExaminationAllocationDO>> list(HttpServletRequest req ,ExaminationAllocationDO examinationAllocationDTO){

		Wrapper<ExaminationAllocationDO> wrapper = new EntityWrapper<ExaminationAllocationDO>().orderBy("id", false);
		if (examinationAllocationDTO.getState() != null){
			wrapper.eq("state",examinationAllocationDTO.getState());
		}
		if (examinationAllocationDTO.getRoomNumber() != null){
			wrapper.like("room_number",examinationAllocationDTO.getRoomNumber());
		}
		if (StringUtils.isNotBlank(examinationAllocationDTO.getEbatchName())){
			Wrapper<ExaminationBatchDO> exWrapper = new EntityWrapper<ExaminationBatchDO>().like("name",examinationAllocationDTO.getEbatchName());
			List<ExaminationBatchDO>  examinationBatchDOS = examinationBatchService.selectList(exWrapper);
			String str = "";
			for (ExaminationBatchDO examinationBatchDO : examinationBatchDOS){
				str = str + examinationBatchDO.getId() + ",";
			}
			wrapper.and();
			if(StringUtils.isNotBlank(str)){
				wrapper.in("ebatch_id",str.substring(0,str.length()-1));
			}else{
				wrapper.eq("ebatch_id",null);
			}
		}
		if (StringUtils.isNotBlank(examinationAllocationDTO.getEroomName())){
			Wrapper<ExaminationRoomDO> exWrapper = new EntityWrapper<ExaminationRoomDO>().like("name",examinationAllocationDTO.getEroomName());
			List<ExaminationRoomDO>  examinationRoomDOS = examinationRoomService.selectList(exWrapper);
			String str = "";
			for (ExaminationRoomDO examinationRoomDO : examinationRoomDOS){
				str = str + examinationRoomDO.getId() + ",";
			}
			wrapper.and();
			if(StringUtils.isNotBlank(str)){
				wrapper.in("eroom_id",str.substring(0,str.length()-1));
			}else{
				wrapper.eq("eroom_id",null);
			}
		}

		//查询用户角色
		String roleName = getRoleNames();
		if (!roleName.contains("超级用户角色")) {
			Object orgId = req.getSession().getAttribute("orgId");
			if (orgId != null){
				wrapper.eq("org_id",orgId);
			}else {
				wrapper.eq("org_id",this.getUser().getOrgId());
			}
		}else {
			if (StringUtils.isNotBlank(examinationAllocationDTO.getOrgName())){
				Wrapper<OrganizationNewsDO> orWrapper = new EntityWrapper<OrganizationNewsDO>().like("name",examinationAllocationDTO.getOrgName());
				List<OrganizationNewsDO>  organizationNewsDOS = organizationNewsService.selectList(orWrapper);
				String str = "";
				for (OrganizationNewsDO organizationNewsDO : organizationNewsDOS){
					str = str + organizationNewsDO.getId() + ",";
				}
				wrapper.and();
				if(StringUtils.isNotBlank(str)){
					wrapper.in("org_id",str.substring(0,str.length()-1));
				}else{
					wrapper.eq("org_id",null);
				}
			}
		}

		Page<ExaminationAllocationDO> page = examinationAllocationService.selectPage(getPage(ExaminationAllocationDO.class), wrapper);
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
			ExaminationAllocationDO obj = (ExaminationAllocationDO) page.getRecords().get(i);
			if (obj.getCreaterId() != null){
				UserDO userDO = userService.selectById(obj.getCreaterId());
				if (userDO != null) {
					obj.setCreaterName(userDO.getUsername());
				}
			}
			if (obj.getEroomId() != null){
				ExaminationRoomDO examinationRoomDO = examinationRoomService.selectById(obj.getEroomId());
				if (examinationRoomDO != null) {
					obj.setEroomName(examinationRoomDO.getName());
				}
			}
			if (obj.getEbatchId() != null){
				ExaminationBatchDO examinationBatchDO = examinationBatchService.selectById(obj.getEbatchId());
				if (examinationBatchDO != null) {
					obj.setEbatchName(examinationBatchDO.getName());
				}
			}
			if (obj.getOrgId() != null){
				OrganizationNewsDO organizationNewsDO = organizationNewsService.selectById(obj.getOrgId());
				if (organizationNewsDO != null) {
					obj.setOrgName(organizationNewsDO.getName());
				}
			}
			list.add(obj);
		}
		page.setRecords(list);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("jzweb:examinationAllocation:add")
	String add(){
	    return "jzweb/examinationAllocation/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:examinationAllocation:edit")
	String edit(@PathVariable("id") Long id,Model model){
		ExaminationAllocationDO examinationAllocation = examinationAllocationService.selectById(id);
		model.addAttribute("examinationAllocation", examinationAllocation);
	    return "jzweb/examinationAllocation/edit";
	}
	
	@Log("添加考场分配")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:examinationAllocation:add")
	public Result<String> save( ExaminationAllocationDO examinationAllocation){
		examinationAllocation.setCreaterId(this.getUserId());
		examinationAllocationService.insert(examinationAllocation);
        return Result.ok();
	}
	
	@Log("修改考场分配")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:examinationAllocation:edit")
	public Result<String>  update( ExaminationAllocationDO examinationAllocation){
		boolean update = examinationAllocationService.updateById(examinationAllocation);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除考场分配")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:examinationAllocation:remove")
	@ApiOperation(value = "竞赛平台-考场分配", notes = "删除考场分配")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "根据id删除", required = true, dataType = "Long"),
	})
	public Result<String>  remove( Long id){
		examinationAllocationService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除考场分配")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:examinationAllocation:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		examinationAllocationService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
